package com.dingwei.lesyndrome.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dingwei.lesyndrome.R;
import com.dingwei.lesyndrome.bean.CarTimeInfoBean;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.SpaceDecoration;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.DisplayUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created time 2018/2/26.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class CoachDetailsAdapter extends RecyclerArrayAdapter<CarTimeInfoBean.DataBean.ListBean> {
    public CoachDetailsAdapter(Context context) {
        super(context);
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new CoachDetailHolder(parent);
    }

    public class CoachDetailHolder extends BaseViewHolder<CarTimeInfoBean.DataBean.ListBean> {


        @Bind(R.id.tv_start_time)
        TextView tvStartTime;
        @Bind(R.id.tv_end_time)
        TextView tvEndTime;
        @Bind(R.id.tv_start_site)
        TextView tvStartSite;
        @Bind(R.id.tv_end_site)
        TextView tvEndSite;
        @Bind(R.id.tv_price)
        TextView tvPrice;
        @Bind(R.id.tv_type)
        TextView tvType;
        @Bind(R.id.tv_count)
        TextView tvCount;
        @Bind(R.id.img_down)
        ImageView imgDown;
        @Bind(R.id.m_recyclerView)
        RecyclerView mRecyclerView;

        CoachDetailsTimeAdapter adapter;
        @Bind(R.id.line)
        View line;
        @Bind(R.id.ll_count)
        LinearLayout llCount;

        public CoachDetailHolder(ViewGroup parent) {
            super(parent, R.layout.item_coach_details);
            ButterKnife.bind(this, itemView);

            adapter = new CoachDetailsTimeAdapter(BaseActivity.context);
            GridLayoutManager layoutManager = new GridLayoutManager(BaseActivity.context, 6);
            layoutManager.setSpanSizeLookup(adapter.obtainGridSpanSizeLookUp(6));
            SpaceDecoration itemDecoration = new SpaceDecoration((int) DisplayUtil.dip2px(BaseActivity.context, 8));//参数是距离宽度
            itemDecoration.setPaddingEdgeSide(true);//是否为左右2边添加padding.默认true.
            itemDecoration.setPaddingStart(true);//是否在给第一行的item添加上padding(不包含header).默认true.
            itemDecoration.setPaddingHeaderFooter(false);//是否对Header于Footer有效,默认false.
            mRecyclerView.setLayoutManager(layoutManager);
            mRecyclerView.setAdapter(adapter);
            mRecyclerView.setNestedScrollingEnabled(false);
            mRecyclerView.addItemDecoration(itemDecoration);

        }

        @Override
        public void setData(CarTimeInfoBean.DataBean.ListBean data) {
            super.setData(data);

            tvStartTime.setText(data.start_time);
            tvEndTime.setText(data.end_time);
            tvStartSite.setText(data.start_site);
            tvEndSite.setText(data.end_site);
            tvPrice.setText("￥" + data.price);
            tvType.setText(data.type);
            if (data.shift_list != null && data.shift_list.size() > 0) {
                tvCount.setText("共" + data.shift_list.size() + "班");
                imgDown.setVisibility(View.VISIBLE);
            } else {
                tvCount.setText("该班车暂无班次信息");
                imgDown.setVisibility(View.GONE);

            }
            adapter.clear();
            adapter.addAll(data.shift_list);

            llCount.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mRecyclerView.getVisibility() == View.VISIBLE){
                        mRecyclerView.setVisibility(View.GONE);
                        line.setVisibility(View.GONE);
                        imgDown.setImageResource(R.drawable.pic49);
                    }else {
                        mRecyclerView.setVisibility(View.VISIBLE);
                        line.setVisibility(View.VISIBLE);
                        imgDown.setImageResource(R.drawable.pic48);
                    }
                }
            });

        }
    }
}
